package com.jiangdong.craft.service.impl;

import com.jiangdong.craft.dto.ResponseDTO;
import com.jiangdong.craft.entity.Procedure;
import com.jiangdong.craft.entity.Relation;
import com.jiangdong.craft.example.CraftProcedureExample;
import com.jiangdong.craft.example.RelationExample;
import com.jiangdong.craft.mapper.CraftProcedureMapper;
import com.jiangdong.craft.mapper.ProcedureMapper;
import com.jiangdong.craft.mapper.RelationMapper;
import com.jiangdong.craft.service.ProcedureService;
import com.jiangdong.craft.util.CraftUtil;
import com.jiangdong.craft.util.ResponseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProcedureServiceImpl implements ProcedureService {

    @Autowired
    ProcedureMapper procedureMapper;
    @Autowired
    CraftProcedureMapper craftProcedureMapper;
    @Autowired
    RelationMapper relationMapper;
    @Autowired
    CraftUtil craftUtil;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO add(Procedure procedure) {
        procedureMapper.insert(procedure);
        return ResponseFactory.getResponse(null,"success");
    }

    /**
     * @param id 要删除节点的id
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO delById(Integer id) {
        //1.删除节点表中的数据
        procedureMapper.deleteByPrimaryKey(id);
        //2.删除节点和流程关系
        CraftProcedureExample craftProcedureExample = new CraftProcedureExample();
        craftProcedureExample.createCriteria().andProcedureIdEqualTo(id);
        craftProcedureMapper.deleteByExample(craftProcedureExample);
        //3.删除节点的关系
        RelationExample relationExample = new RelationExample();
        relationExample.createCriteria().andProcedureIdEqualTo(id);
        relationMapper.deleteByExample(relationExample);

        List<Relation> relations = relationMapper.selectByExample(null);

        List<Relation> relationList = new ArrayList<>();

        for (Relation relation : relations){
            if (relation.getProcedureId().equals(id) || relation.getPostpostitionId().equals(id)){
                if (relation.getProcedureId().equals(id)){
                    relation.setProcedureId(null);
                }
                if (relation.getPostpostitionId().equals(id)){
                    relation.setPostpostitionId(null);
                }

                relationList.add(relation);
            }
        }

        relationMapper.updateBatch(relationList);

        return ResponseFactory.getResponse(null,"success");
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO query(Integer id,Integer craftId) {
        Procedure procedure = procedureMapper.selectByPrimaryKey(id);
        if (craftId != null){
            List<Relation> relationList = relationMapper.selectByExample(null);
            procedure = craftUtil.saveRelation(relationList,procedure,craftId);
        }
        return ResponseFactory.getResponse(procedure,"success");
    }

}
